package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.mall.lib.domain.MallWechatConfig;
import com.rockcent.mall.lib.dto.MerchantDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2016/12/04.
 */
@Component
public class WechatConfigRepositoryImpl implements WechatConfigRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(WechatConfigRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public MallWechatConfig queryWechatConfigByDomain(String domain) {
        SQLObjectDoing<MallWechatConfig> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s  from mall m ,mall_wechat_config w where m.id = w.mall_id and m.`mobile_host_name`= :domain limit 1 ");
            params.put("domain", domain);
            fields.append(" w.* ");
            log.debug(sql.toString());
        };
        return result.go(MallWechatConfig.class, jdbcTemplate, log, objectMapper);
    }
}
